package com.bingxu.flink.advancedcode;

import com.bingxu.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.ProcessingTimeSessionWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * @author :BingXu
 * @description :TODO
 * @date :2021/8/16 19:10
 * @modifier :
 */

public class WindowAggre {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);

        env
                .socketTextStream("localhost", 9999)
                .map((MapFunction<String, WaterSensor>) value -> {
                    String[] fields = value.split(",");
                    return new WaterSensor(fields[0], Long.valueOf(fields[1]), Integer.valueOf(fields[2]));
                })
                .keyBy(ele -> ele.getId())
                .window(ProcessingTimeSessionWindows.withGap(Time.seconds(5)))
                .aggregate(new AggregateFunction<WaterSensor, Tuple2<Integer,Long>, Integer>() {
                    /**
                     * TODO 初始化一个累加器
                     * @return
                     */
                    @Override
                    public Tuple2<Integer, Long> createAccumulator() {
                        return Tuple2.of(0,0L);
                    }

                    /**
                     * TODO 计算累加器的逻辑
                     * @param value
                     * @param accumulator
                     * @return
                     */
                    @Override
                    public Tuple2<Integer, Long> add(WaterSensor value,
                                                     Tuple2<Integer, Long> accumulator) {

                        return Tuple2.of(accumulator.f0+value.getVc(), accumulator.f1+value.getTs());
                    }

                    /**
                     * TODO 返回累计结果
                     * @param accumulator
                     * @return
                     */
                    @Override
                    public Integer getResult(Tuple2<Integer, Long> accumulator) {
                        return accumulator.f0;
                    }

                    /**
                     * 多流的时候merge流的累加器数据
                     * @param a
                     * @param b
                     * @return
                     */
                    @Override
                    public Tuple2<Integer, Long> merge(Tuple2<Integer, Long> a, Tuple2<Integer, Long> b) {
                        return null;
                    }
                })
                .print();

        env.execute();
    }
}
